{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import difflib"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_equal_rate(str1, str2):\n",
    "   return difflib.SequenceMatcher(None, str1, str2).quick_ratio()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "s1 = u\"asdf\\xa0fffaaa  d\"\n",
    "s2 = \"asdffffaaad\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.88"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_equal_rate(s1, s2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [],
   "source": [
    "str1='光伏科学技术的开发（非研制）、咨询、交流、推广，太阳能电池（光伏电池）制造、销售，太阳能发电，新能源发电工程设计、施工服务，太阳能电站系统的技术咨询、转让、推广服务，太阳能系统设备的安装、调试、维护和技术支持服务，自营和代理各类商品及技术的进出口业务（国家限定企业经营或禁止进出口的商品及技术除外）。光伏设备及元器件制造；光伏设备及元器件销售；（依法须经批准的项目，经相关部门批准后方可开展经营活动）'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [],
   "source": [
    "df = pd.read_csv('2017版(含附录)国民经济行业分类(GBT4754—2017).csv')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['相似度'] = df['说明'].apply(get_equal_rate, str2=str1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>门类</th>\n",
       "      <th>大类</th>\n",
       "      <th>中类</th>\n",
       "      <th>小类</th>\n",
       "      <th>代码</th>\n",
       "      <th>说明</th>\n",
       "      <th>相似度</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>153</th>\n",
       "      <td>C</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>NaN</td>\n",
       "      <td>制造业</td>\n",
       "      <td>本门类包括13～43大类，指经物理变化或化学变化后成为新的产品，不论是动力机械制造或手工...</td>\n",
       "      <td>0.318777</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    门类  大类  中类  小类   代码                                                 说明  \\\n",
       "153  C NaN NaN NaN  制造业    本门类包括13～43大类，指经物理变化或化学变化后成为新的产品，不论是动力机械制造或手工...   \n",
       "\n",
       "          相似度  \n",
       "153  0.318777  "
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df[df['相似度']==df['相似度'].max()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
